/**
 * Created by no1 on 2015/7/24.
 */

'use strict';
let F = require('../function');
let DD = require('./daoLoader');
let CategoryMongoDao = new DD.mongoDB.CategoryDao();
let ShopMongoDao = new DD.mongoDB.ShopDao();
//var _ = require('underscore');
//_.str = require('underscore.string');
//_.v = require('validator');

/**
 * @todo constructor
 * @param data
 * @constructor
 */
function Base(data) {
  //this.model = data.Model || {};
  this.dao = data.dao || {};
  this.esDao = data.esDao || {};
  this.redisDao = data.redisDao || {};
  this.modelName = data.modelName || '';
  this.modelType = data.modelType || '';
}

/**
 *@todo 根据id集合搜索列表
 *@param  {Object}  IDArray  所商家的id集合
 *@ret    {Object}  商家的列表
 */
Base.prototype.getByIdArray = function*(IDArray) {
  let ret = yield this.dao.getByIdArr(IDArray);
  if (ret.error == 1) return F.returnData(1, ret.msg)
  return F.returnData(0, ret.msg, ret.data);

};

/**
 *@todo 查询一个
 *@param  {Object}  query  查询的对象
 *@ret    {Object}  商家的列表
 */
Base.prototype.getOneByQuery = function*(query, opt) {
  let ret = yield this.dao.getOneByQuery(query, opt);
  if (ret.error == 1) return F.returnData(1, ret.msg)
  return F.returnData(0, ret.msg, ret.data);

};

/**
 *@todo 查询
 *@param  {Object}  query  查询的对象
 *@ret    {Object}  商家的列表
 */
Base.prototype.getByQuery = function*(query, opt, page, pageSize) {
  let ret = yield this.dao.getByQuery(query, opt, page, pageSize);
  if (ret.error == 1) return F.returnData(1, ret.msg)
  return F.returnData(0, ret.msg, ret.data);

};

/**
 * todo 内部调用
 * @param Que
 * @returns {{error, msg, data}|{error, msg}}
 */
Base.prototype.findByDis = function*(Que) {
  let arrDel = ['_id', '__v', '__t', 'createdAt', 'updateAt', 'status']
    , opt = {};
  for (let one of arrDel) opt[one] = 0;
  let ret = yield this.dao.getByQuery(Que, opt);
  if (ret.error == 1) F.returnData(1, ret.msg, ret.data);
  return F.returnData(0, ret.msg, ret.data);
}

Base.prototype.findCountByDis = function*(Que) {
  let ret = yield this.dao.getCountByQuery(Que);
  let data = ret.data || 0;
  return F.returnData(0, ret.msg, data);
}

module.exports = Base;